package com.example.account.controller;

import com.example.account.domain.Account;
import com.example.account.domain.User;
import com.example.account.repository.AccountRepository;
import com.example.account.repository.UserRepository;
import com.example.account.service.AccountService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.*;

@RestController
public class AccountController {

    @Autowired
    private AccountRepository accountRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private AccountService accountService;

    //获得账户所有账单
    @RequestMapping(value = "/getBills", method = RequestMethod.GET)
    public Object getBills(@RequestParam("tel") String tel,
                           @RequestParam("date") String date
    ) {
        List<Map> list = new ArrayList<>();

        for (Account i : accountRepository.findAll()) {
            Map<Object, Object> map = new HashMap<>();
            if (tel.equals(i.getTel())) {
                map.put("id", i.getId());
                map.put("notes", i.getNotes());
                map.put("type", i.getType());
                map.put("money", i.getMoney());
                map.put("date", i.getDate());
                map.put("icon", i.getIcon());
                map.put("text", i.getText());
                map.put("iconSelected", i.getIconSelected());

                String date1 = i.getDate().toString();
                String date2 = date1.substring(0, 7);

                if (date.equals(date2)) {
                    list.add(map);
                }
            }
        }
        return list;
    }

    //获取年账单、及个月显示
    @RequestMapping(value = "/getBillsyear", method = RequestMethod.GET)
    public Object getBillsyear(@RequestParam("tel") String tel,
                               @RequestParam("date") String date
    ) {
        String[] a = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        Map<Object, Object> map = new HashMap<>();
        Map<Object, Object> map1 = new HashMap<>();
        List<Map> list = new ArrayList<>();
        Double mon1 = 0.00;//支出
        Double mon2 = 0.00;//收入
        Double mon3 = 0.00;//剩余
        Double nsr = 0.00;//年收入
        Double nzc = 0.00;//年支出
        Double njy = 0.00;//年结余
        for (Account i : accountRepository.findAll()) {
            if (tel.equals(i.getTel())) {
                String data1 = i.getDate().toString().substring(0, 4);
                String date3 = date.substring(0, 4);
                if (date3.equals(data1)) {
                    if ("0".equals(i.getType())) {
                        nzc = nzc + i.getMoney();
                    } else {
                        nsr = nsr + i.getMoney();
                    }
                }
            }
        }
        njy = nsr - nzc;
        DecimalFormat df = new DecimalFormat("0.00");

        for (int j = 0; j < a.length; j++) {
            mon1 = 0.00;//支出
            mon2 = 0.00;//收入
            mon3 = 0.00;//剩余
            map = new HashMap<>();
            for (Account i : accountRepository.findAll()) {
                if (tel.equals(i.getTel())) {
                    String data1 = i.getDate().toString().substring(0, 4);
                    String date3 = date.substring(0, 4);
                    if (date3.equals(data1)) {
                        String month = i.getDate().toString().substring(5, 7);

                        if (a[j].equals(month)) {
                            if ("0".equals(i.getType())) {
                                mon1 = mon1 + i.getMoney();
                            } else {
                                mon2 = mon2 + i.getMoney();
                            }
                        }
                    }
                }
            }
            map.put("num", a[j]);
            map.put("mon1", df.format(mon1));
            map.put("mon2", df.format(mon2));
            map.put("mon3", df.format(mon2 - mon1));
            list.add(map);
        }
        map1.put("list", list);
        map1.put("nzc", nzc);
        map1.put("nsr", nsr);
        map1.put("njy", njy);
        return map1;
    }


    //获取账单记录（table使用）
    @RequestMapping(value = "/getBills1", method = RequestMethod.GET)
    public Object getBills1(@RequestParam("tel") String tel,
                            @RequestParam("date") String date,
                            @RequestParam("type") String type
    ) {
        List<Map> list = new ArrayList<>();

        for (Account i : accountRepository.findAll()) {
            Map<Object, Object> map = new HashMap<>();
            if (tel.equals(i.getTel())) {
                if (i.getType().equals(type)) {
                    map.put("id", i.getId());
                    map.put("notes", i.getNotes());
                    map.put("type", i.getType());
                    map.put("money", i.getMoney());
                    map.put("date", i.getDate());
                    map.put("icon", i.getIcon());
                    map.put("text", i.getText());
                    map.put("iconSelected", i.getIconSelected());

                    String date1 = i.getDate().toString();
                    String date2 = date1.substring(0, 7);

                    if (date.equals(date2)) {
                        list.add(map);
                    }
                }
            }
        }
        return list;
    }

    //添加记录
    @PostMapping(value = "/addBills")
    public Object addBills(
            @RequestParam("date") String date,
            @RequestParam("iconSelected") Integer iconSelected,
            @RequestParam("notes") String notes,
            @RequestParam("money") Double money,
            @RequestParam("tel") String tel,
            @RequestParam("type") String type,
            @RequestParam("text") String text,
            @RequestParam("icon") String icon


    ) {
        Account account = new Account();
        account.setDate(date);
        account.setIconSelected(iconSelected);
        account.setNotes(notes);
        account.setMoney(money);
        account.setTel(tel);
        account.setType(type);
        account.setIcon(icon);
        account.setText(text);
        //account.setCreateAt(createAt);
        accountRepository.save(account);
        return "添加成功！";
    }

    //修改记录
    @PostMapping(value = "/alterBills")
    public Object alterBills(@RequestParam("id") Long id,
                             @RequestParam("date") String date,
                             @RequestParam("iconSelected") Integer iconSelected,
                             @RequestParam("notes") String notes,
                             @RequestParam("money") Double money,
                             @RequestParam("tel") String tel,
                             @RequestParam("type") String type,
                             @RequestParam("text") String text,
                             @RequestParam("icon") String icon

    ) {
        Account account = new Account();
        account.setId(id);
        account.setDate(date);
        account.setIconSelected(iconSelected);
        account.setNotes(notes);
        account.setMoney(money);
        account.setTel(tel);
        account.setType(type);
        account.setIcon(icon);
        account.setText(text);
        accountRepository.save(account);
        return "修改成功！";
    }

    //删除某一账单信息
    @RequestMapping(value = "/delBills", method = RequestMethod.GET)
    public Object delBills(@RequestParam("id") Long id) {
        accountRepository.deleteById(id);
        return "已删除！";

    }

    @PostMapping(value = "/findBills")
    public Object findBills(@RequestParam("notes") String notes,
                            @RequestParam("tel") String tel) {

        List<Map> list1 = new ArrayList<>();
        for (Account i : accountRepository.findAll()) {
            if (tel.equals(i.getTel())) {
                Map<Object, Object> map1 = new HashMap<>();
                if (i.getNotes().equals(notes)) {
                    map1.put("id", i.getId());
                    map1.put("notes", i.getNotes());
                    map1.put("type", i.getType());
                    map1.put("money", i.getMoney());
                    map1.put("date", i.getDate());
                    map1.put("icon", i.getIcon());
                    map1.put("text", i.getText());
                    map1.put("iconSelected", i.getIconSelected());
                    list1.add(map1);
                }
            }
        }
        return list1;
    }

}
